home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / µSim 1.1 / FabLibsƒ / Independents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-24  |  11.5 KB  |  548 lines  |  [TEXT/CWIE]

  1. #include    <Limits.h>
  2. #include    "Independents.h"
  3.  
  4. #ifndef    MIN
  5. #define MIN(a, b)    ((a) < (b) ? (a) : (b))
  6. #endif
  7.  
  8. static void FrameRoundBW(const RectPtr theRect, ConstPatternParam bpat, short ovalP);
  9.  
  10.  
  11. static ControlHandle    gToBeHilited = nil;
  12.  
  13.  
  14. UInt32 sizeAfterPaste(TEHandle activeTE, UInt32 howMuch)
  15. {
  16. return InlineGetHandleSize(TEGetText(activeTE)) - ((*activeTE)->selEnd - (*activeTE)->selStart) + howMuch;
  17. }
  18.  
  19.  
  20. UInt32 FindCommonPrefix(Ptr p, UInt32 lenp, Ptr q, UInt32 lenq)
  21. {
  22. Ptr        limit;
  23. UInt32    prefixLen = 0;
  24.  
  25. limit = q + MIN(lenp, lenq);
  26. while (q < limit) {
  27.     if (*p++ == *q++)
  28.         ++prefixLen;
  29.     else
  30.         break;
  31.     }
  32.  
  33. return prefixLen;
  34. }
  35.  
  36.  
  37. #ifndef GENERATENODATA
  38. #define GENERATENODATA 0
  39. #endif
  40.  
  41. Boolean    HasDragManager(void)
  42. {
  43. long            response;
  44. #if !GENERATENODATA
  45. static Boolean    tested = false;
  46. static    
  47. #endif    /* !GENERATENODATA */
  48. Boolean    result = false;
  49.     
  50. #if !GENERATENODATA
  51. if ( !tested ) {
  52.     tested = true;
  53. #endif    /* !GENERATENODATA */
  54.     if (Gestalt(gestaltDragMgrAttr, &response) == noErr) {
  55.         result = response & (1L << gestaltDragMgrPresent);
  56. #if GENERATINGCFM
  57.         if (result)
  58.             if (InstallTrackingHandler == (void *)kUnresolvedCFragSymbolAddress)
  59.                 result = false;
  60. #endif
  61.         }
  62. #if !GENERATENODATA
  63.     }
  64. #endif    /* !GENERATENODATA */
  65.  
  66. return result;
  67. }
  68.  
  69. void SetDialogItemHandleText(DialogRef d, Handle text, short itemNo)
  70. {
  71. Str255    theS;
  72. Rect    tempRect;
  73. Handle    item;
  74. UInt32    len;
  75. short    type;
  76.  
  77. len = InlineGetHandleSize(text);
  78. if (len <= UCHAR_MAX) {    // can use safe OS call
  79.     StrLength(theS) = len;
  80.     BlockMoveData(*text, &theS[1], len);
  81.     GetDialogItem(d, itemNo, &type, &item, &tempRect);
  82.     SetDialogItemText(item, theS);
  83.     }
  84. else {    // must use potentially dangerous trick that works, though (-: :-)
  85.     HLockHi(text);
  86.     TESetText(*text, len, ((DialogPeek)d)->textH);
  87.     HUnlock(text);
  88.     }
  89. }
  90.  
  91. void SetDialogItemLongText(DialogRef d, const Ptr text, UInt32 len, short itemNo)
  92. {
  93. Str255    theS;
  94. Rect    tempRect;
  95. Handle    item;
  96. short    type;
  97.  
  98. if (len <= UCHAR_MAX) {    // can use safe OS call
  99.     StrLength(theS) = len;
  100.     BlockMoveData(text, &theS[1], len);
  101.     GetDialogItem(d, itemNo, &type, &item, &tempRect);
  102.     SetDialogItemText(item, theS);
  103.     }
  104. else {    // must use potentially dangerous trick that works, though (-: :-)
  105.     TESetText(text, len, ((DialogPeek)d)->textH);
  106.     }
  107. }
  108.  
  109. void FlashButton(DialogPtr dlg, short item)
  110. {
  111. Rect    txtBox;
  112. ControlHandle    itH;
  113. long    dummy;
  114. short    typ;
  115.  
  116. GetDialogItem(dlg, item, &typ, (Handle *)&itH, &txtBox);
  117. HiliteControl(itH, 1);
  118. Delay(4L, &dummy);
  119. HiliteControl(itH, 0);
  120. }
  121.  
  122. void BeginFlashButton(DialogPtr dlg, short item)
  123. {
  124. Rect    txtBox;
  125. short    typ;
  126.  
  127. GetDialogItem(dlg, item, &typ, (Handle *)&gToBeHilited, &txtBox);
  128. HiliteControl(gToBeHilited, 1);
  129. }
  130.  
  131. void EndFlashButton(void)
  132. {
  133. if (gToBeHilited) {
  134.     HiliteControl(gToBeHilited, 0);
  135.     gToBeHilited = nil;
  136.     }
  137. }
  138.  
  139. enum {
  140. kIsColorPort = 0xC000,
  141. kButtonFrameSize = 3,    /* button frame's pen size */
  142. kButtonFrameInset = -4    /* inset rectangle adjustment around button */
  143. };
  144.  
  145. void OutlineButton(DialogPtr oftheDialog, short myItem)
  146. {
  147. RGBColor    foreCol, backCol, savedForeCol;
  148. PenState    curPen;
  149. PatPtr    thePat;
  150. GrafPtr    savePort;
  151. Rect    txtBox;
  152. Handle    itH;
  153. short    typ;
  154.  
  155. GetDialogItem(oftheDialog, myItem, &typ, &itH, &txtBox);
  156. GetPort(&savePort);
  157. SetGrafPortOfDialog(oftheDialog);
  158.  
  159. InsetRect(&txtBox, kButtonFrameInset, kButtonFrameInset);
  160. typ = ((txtBox.bottom - txtBox.top) >> 1) + 2;
  161.  
  162. thePat = (*(ControlHandle)itH)->contrlHilite == 0 ? &qd.black : &qd.gray;
  163.  
  164. GetPenState(&curPen);
  165. PenNormal();
  166. PenSize(kButtonFrameSize, kButtonFrameSize);
  167. if ((((CGrafPtr)(*(ControlHandle)itH)->contrlOwner)->portVersion & kIsColorPort) == kIsColorPort) {
  168.     if ((*(ControlHandle)itH)->contrlHilite) {
  169.         GetForeColor(&foreCol);
  170.         savedForeCol = foreCol;
  171.         GetBackColor(&backCol);
  172.         if (GetGray(GetGDevice(), &backCol, &foreCol)) {
  173.             RGBForeColor(&foreCol);
  174.             FrameRoundRect(&txtBox, typ, typ);
  175.             RGBForeColor(&savedForeCol);
  176.             }
  177.         else
  178.             FrameRoundBW(&txtBox, thePat, typ);
  179.         }
  180.     else
  181.         FrameRoundRect(&txtBox, typ, typ);
  182.     }
  183. else {
  184.     FrameRoundBW(&txtBox, thePat, typ);
  185.     }
  186.  
  187. SetPenState(&curPen);
  188. SetPort(savePort);
  189. }
  190.  
  191. void FrameRoundBW(const RectPtr theRect, ConstPatternParam bpat, short ovalP)
  192. {
  193. PenPat(bpat);
  194. FrameRoundRect(theRect, ovalP, ovalP);
  195. }
  196.  
  197. void DoMovableModelessUpdate(DialogRef dPtr, short defaultItem)
  198. {
  199. UpdateDialog(dPtr, dPtr->visRgn);
  200. if (defaultItem)
  201.     OutlineButton(dPtr, defaultItem);
  202. }
  203.  
  204.  
  205. // GetPtrIndHString accepts a Handle to a STR# resource and an index;
  206. // it returns a pointer to the pascal string at that index.
  207. // It does not move memory.
  208. // If you passed an unlocked handle, the pointer is valid as long
  209. // as you don't move memory.
  210. // Useful when you need only read the string.
  211.  
  212. StringPtr GetPtrIndHString(Handle resH, unsigned short index)
  213. {
  214. unsigned short    *compPtr = (unsigned short *)*resH;
  215. unsigned char    *spanPtr;
  216. unsigned short    i = index;
  217.  
  218. if (*compPtr++ <= i)
  219.     spanPtr = nil;
  220. else {
  221.     spanPtr = (unsigned char *)compPtr;
  222.     while (i) {
  223.         spanPtr += *spanPtr++;
  224.         i--;
  225.         }
  226.     }
  227.  
  228. return (StringPtr)spanPtr;
  229. }
  230.  
  231.  
  232. // GetIndHString accepts a Handle to a STR# resource and an index;
  233. // it copies the pascal string into dest.
  234. // It does not move memory. Thus, it's OK to pass an unlocked handle.
  235. // Useful when you need to modify the string subsequently.
  236.  
  237. void GetIndHString(StringPtr dest, Handle resH, unsigned short index)
  238. {
  239. unsigned short    *compPtr = (unsigned short *)*resH;
  240. unsigned char    *spanPtr;
  241. unsigned short    i = index;
  242.  
  243. if (*compPtr++ <= i)
  244.     StrLength(dest) = 0;
  245. else {
  246.     spanPtr = (unsigned char *)compPtr;
  247.     while (i) {
  248.         spanPtr += *spanPtr++;
  249.         i--;
  250.         }
  251.     BlockMoveData(spanPtr, dest, *spanPtr + 1L);
  252.     }
  253.  
  254. return;
  255. }
  256. /*
  257. short KeyState(unsigned short k )
  258. // k =  any keyboard scan code, 0-127
  259. {
  260. KeyMap    km;
  261.  
  262. GetKeys(km);
  263. return ( ( *((unsigned char *)km + (k>>3)) >> (k & 7) ) & 1);
  264. }
  265. */
  266.  
  267. Boolean ModifiersState(EventModifiers mask)
  268. {
  269. EventRecord    ev;
  270.  
  271. (void) EventAvail(0, &ev);
  272. return (ev.modifiers & mask) != 0;
  273. }
  274.  
  275. OSType Str2OSType(ConstStr255Param theStr)
  276. {
  277. OSType result;
  278. Ptr    source, dest;
  279. unsigned char i = StrLength(theStr);
  280.  
  281. source = (Ptr)theStr + 1;
  282. dest = (Ptr)&result;
  283. *dest++ = *source++;
  284. *dest++ = *source++;
  285. *dest++ = *source++;
  286. *dest++ = *source++;
  287.  
  288. if (i < 4)
  289.     while (i < 4) {
  290.         *--dest = 0x20;
  291.         i++;
  292.         }
  293.  
  294. return result;
  295. }
  296.  
  297. void OSType2Str(OSType typ, StringPtr theStr)
  298. {
  299. register StringPtr    tempPtr = theStr;
  300. register StringPtr    sourcePtr = (StringPtr)&typ;
  301.  
  302. *tempPtr++ = 4;
  303. *tempPtr++ = *sourcePtr++;
  304. *tempPtr++ = *sourcePtr++;
  305. *tempPtr++ = *sourcePtr++;
  306. *tempPtr = *sourcePtr;
  307. }
  308.  
  309. /* this is taken from "Macintosh Programming Secrets"
  310. by Scott Knaster & Keith Rollin;
  311. 96/09/17 FO: improved according to TN TE 23: International Canceling
  312. optimizations by Fabrizio Oddone */
  313.  
  314. Boolean CmdPeriod(const EventRecord * const theEvent)
  315. {
  316. #define    kModifiersMask    (0xFF00 & ~cmdKey)
  317.  
  318. Handle    hKCHR = nil;
  319. Ptr        KCHRPtr;
  320. UInt32    state;
  321. UInt32    keyInfo;
  322. UInt16    keyCode;
  323. Boolean    result = false;
  324.  
  325. if (theEvent->what == keyDown || theEvent->what == autoKey) {
  326.     if (theEvent->modifiers & cmdKey) {
  327.         keyCode = (theEvent->modifiers & kModifiersMask) | ((unsigned short)theEvent->message >> 8);
  328.         state = 0;
  329.         KCHRPtr = (Ptr) GetScriptManagerVariable(smKCHRCache);
  330.         if (KCHRPtr == nil) {
  331.             hKCHR = GetResource('KCHR', GetScriptVariable(GetScriptManagerVariable(smKeyScript), smScriptKeys));
  332.             KCHRPtr = *hKCHR;
  333.             }
  334.  
  335.         keyInfo = KCHRPtr ? KeyTranslate(KCHRPtr, keyCode, &state) : theEvent->message;
  336.  
  337.         if (hKCHR)
  338.             ReleaseResource(hKCHR);
  339.  
  340.         if (((char)keyInfo == '.') || ((char)(keyInfo >> 16) == '.'))
  341.             result = true;
  342.         }
  343.     }
  344.  
  345. return result;
  346. }
  347.  
  348. Handle FabGetFullPath(const FSSpecPtr fss, Boolean AUXpresent)
  349. {
  350. CInfoPBRec    mypb;
  351. Str63    dirName;
  352. Handle    fullPath;
  353. //UInt32    hCurSize;
  354. OSErr    err = noErr;
  355.  
  356. fullPath = NewHandle(StrLength(fss->name));
  357. if (fullPath) {
  358.     BlockMoveData(fss->name + 1, *fullPath, StrLength(fss->name));
  359.     if (fss->parID != fsRtParID) {
  360.         mypb.dirInfo.ioNamePtr = dirName;
  361.         mypb.dirInfo.ioVRefNum = fss->vRefNum;
  362.         mypb.dirInfo.ioDrParID = fss->parID;
  363.         mypb.dirInfo.ioFDirIndex = -1;
  364.         do {
  365.             mypb.dirInfo.ioDrDirID = mypb.dirInfo.ioDrParID;
  366.             err = PBGetCatInfoSync(&mypb);
  367.             if (err)
  368.                 break;
  369.             else {
  370.                 if (AUXpresent) {
  371.                     if (dirName[1] != '/') {
  372.                         StrLength(dirName) += 1;
  373.                         dirName[StrLength(dirName)] = '/';
  374.                         }
  375.                     }
  376.                 else {
  377.                     StrLength(dirName) += 1;
  378.                     dirName[StrLength(dirName)] = ':';
  379.                     }
  380. /*                hCurSize = InlineGetHandleSize(fullPath);
  381.                 SetHandleSize(fullPath, hCurSize + StrLength(dirName));
  382. */
  383.                 /* Add directory name to beginning of fullPath */
  384.                 (void) Munger(fullPath, 0, NULL, 0, &dirName[1], StrLength(dirName));
  385.                 err = MemError();
  386.                 if (err)
  387.                     break;
  388. /*                else {
  389.                     BlockMoveData(*fullPath, *fullPath + StrLength(dirName), hCurSize);
  390.                     BlockMoveData(dirName + 1, *fullPath, StrLength(dirName));
  391.                     }
  392. */                }
  393.             }
  394.         while (mypb.dirInfo.ioDrDirID != fsRtDirID);
  395.         }
  396.     }
  397. if (err) {
  398.     DisposeHandle(fullPath);
  399.     fullPath = nil;
  400.     }
  401. return fullPath;
  402. }
  403.  
  404. void fabc2pstr(unsigned char *cs, StringPtr destps)
  405. {
  406. unsigned char *scanPtr = cs;
  407. unsigned char *destPtr = destps + 1;
  408. unsigned int count = 0;
  409.  
  410. while ((*destPtr++ = *scanPtr++) && count < UCHAR_MAX)
  411.     count++;
  412. StrLength(destps) = count;
  413. }
  414.  
  415. Boolean IsOnScreen(const Rect * const r)
  416. {
  417. Point    topRight;
  418.  
  419. topRight.h = r->right;
  420. topRight.v = r->top;
  421. return (PtInRgn(topLeft(*r), GetGrayRgn()) || PtInRgn(topRight, GetGrayRgn()));
  422. }
  423.  
  424. Boolean IsOnScreenWeak(Point pt)
  425. {
  426. Rect    box;
  427.  
  428. topLeft(box) = pt;
  429. box.bottom = box.top + qd.thePort->portRect.bottom - qd.thePort->portRect.top;
  430. box.right = box.left + qd.thePort->portRect.right - qd.thePort->portRect.left;
  431. return IsOnScreen(&box);
  432. }
  433.  
  434. #pragma segment Init
  435.  
  436. void SetupStringFromPrefs(StringHandle *gString, short strID)
  437. {
  438. register Handle    tempH;
  439.  
  440. tempH = (Handle)GetString(strID);
  441. if (tempH) {
  442.     DetachResource(tempH);
  443.     if (*gString)
  444.         DisposeHandle((Handle)*gString);
  445.     *gString = (StringHandle)tempH;
  446.     }
  447. }
  448.  
  449. UInt32 SetupULongFromPrefs(short strID)
  450. {
  451. register Handle    tempH;
  452. UInt32    theCount = 0UL;
  453.  
  454. tempH = Get1Resource('ULNG', strID);
  455. if (tempH) {
  456.     theCount = *(UInt32 *)*tempH;
  457.     ReleaseResource(tempH);
  458.     }
  459. return theCount;
  460. }
  461.  
  462. Boolean SetupBooleanFromPrefs(short strID)
  463. {
  464. register Handle    tempH;
  465. Boolean    theCount = false;
  466.  
  467. tempH = Get1Resource('BOOL', strID);
  468. if (tempH) {
  469.     theCount = *(Boolean *)*tempH;
  470.     ReleaseResource(tempH);
  471.     }
  472. return theCount;
  473. }
  474.  
  475. Boolean FinderIsFront(void)
  476. {
  477. long                response;
  478. ProcessSerialNumber    process;
  479. ProcessInfoRec        info;
  480. Boolean                front = false;
  481.  
  482. if (Gestalt(gestaltOSAttr, &response) == noErr && (response & (1L << gestaltLaunchControl))) {
  483.     if (GetFrontProcess(&process) == noErr) {
  484.         info.processInfoLength  = sizeof(ProcessInfoRec);
  485.         info.processName        = 0L;
  486.         info.processAppSpec     = 0L;
  487.         if (GetProcessInformation(&process, &info) == noErr && info.processSignature == 'MACS')
  488.             front = true;
  489.         }
  490.     }
  491. return front;
  492. }
  493.  
  494. #pragma segment CleanUp
  495.  
  496. void AddString(StringHandle theAddedStr, short strID)
  497. {
  498. register Handle tempH;
  499.  
  500. SetResLoad(false);
  501. tempH = Get1Resource('STR ', strID);
  502. SetResLoad(true);
  503. if (tempH) {
  504.     RemoveResource(tempH);
  505.     DisposeHandle(tempH);
  506.     }
  507. if (theAddedStr)
  508.     AddResource((Handle)theAddedStr, 'STR ', strID, "\p");
  509. }
  510.  
  511. void AddULong(UInt32 theAddedL, short strID)
  512. {
  513. register Handle tempH;
  514.  
  515. SetResLoad(false);
  516. tempH = Get1Resource('ULNG', strID);
  517. SetResLoad(true);
  518. if (tempH) {
  519.     RemoveResource(tempH);
  520.     DisposeHandle(tempH);
  521.     }
  522. tempH = NewHandle(sizeof(UInt32));
  523. if (tempH) {
  524.     *(UInt32 *)*tempH = theAddedL;
  525.     AddResource(tempH, 'ULNG', strID, "\p");
  526.     }
  527. }
  528.  
  529. void AddBoolean(Boolean theAddedL, short strID)
  530. {
  531. register Handle tempH;
  532.  
  533. SetResLoad(false);
  534. tempH = Get1Resource('BOOL', strID);
  535. SetResLoad(true);
  536. if (tempH) {
  537.     RemoveResource(tempH);
  538.     DisposeHandle(tempH);
  539.     }
  540. tempH = NewHandle(sizeof(Boolean));
  541. if (tempH) {
  542.     *(Boolean *)*tempH = theAddedL;
  543.     AddResource(tempH, 'BOOL', strID, "\p");
  544.     }
  545. }
  546.  
  547. // check out the last #pragma segment !!!!!
  548.